package face.pay.api.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.face.pay.IPayConfigServer;
import face.pay.api.payter.channel.paytype.wechat.api.ApiWxPayQuery;
import face.pay.api.wechat.service.IPayOrderService;
import face.pay.constant.ChannelCode;
import face.pay.constant.OemType;
import face.pay.constant.OrderStatus;
import face.pay.dao.*;
import face.pay.entity.OrderInfo;
import face.pay.rocket.MqConfig;
import face.pay.util.MathUtil;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: cjj
 * @Date: 2019/9/10
 * @Description:
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends AbstractOrderSafeServiceImpl implements IPayOrderService {

    private static final String domain = "api.mch.weixin.qq.com";

    private static final  String urlSuffix = "/pay/micropay";


    @Autowired
    private TerminalInfoDao terminalInfoDao;

    @Autowired
    private StoreInfoDao storeInfoDao;

    @Autowired
    private EnterpriseInfoDao enterpriseInfoDao;

    @Autowired
    private OrderInfoDao orderInfoDao;

    @Autowired
    private OemInfoDao oemInfoDao;


    @Autowired
    ProducerBean producerBean;

    @Autowired
    MqConfig mqConfig;

    @Autowired
    ApiWxPayQuery wxQrPayQueryServer;


    @Autowired
    private IPayConfigServer iWxPayConfigServer;

    @Override
    @Transactional
    public ResultMessage checkOrderStatus(String dataJson) {

        log.info("扫码支付订订单结果查询"+dataJson);

        Map<String,Object> map = JSON.parseObject(dataJson);

        String out_trade_no = map.get("out_trade_no")==null?"0":map.get("out_trade_no").toString();

        Long orderId  = Long.valueOf(out_trade_no);

        OrderInfo orderInfo = this.orderInfoDao.getById(orderId);

        String status = OrderStatus.NOTPAY;

        Map<String,String> resultMap =  new HashMap<>();


        if(orderInfo!=null){

            Integer order_status =  orderInfo.getStatus();

            status =  OrderStatus.getStatus(order_status);

            resultMap.put("out_trade_no",out_trade_no);
            resultMap.put("status",status);
            Long totalFee = orderInfo.getTotalFee();

            String message  = orderInfo.getMessage();

            if(ChannelCode.WECHAT.equals(orderInfo.getChannelCode())){
                if(OrderStatus.SUCCESS.equals(status)){
                    message += MathUtil.transForMoney(totalFee)+"元";

                }
            }
            resultMap.put("status_desc",message);


            ResultMessage resultMessage =   ResultMessage.createSuccess(resultMap);

            String  result = JSON.toJSONString(resultMessage);

            log.info(result);

            return  resultMessage;


        }else {
            return  ResultMessage.createFail("订单不存在");
        }



    }

    private void sendMessage(OrderInfo orderInfo, String type) {

        String json = JSON.toJSONString(orderInfo);

        String topic = mqConfig.getOrderTopic();

        if (OemType.OPEN.equals(topic)) {
            topic = mqConfig.getCallBackOrderTopic();
        }

        Message message = new Message(topic, mqConfig.getProfitTag(), json.getBytes());

        try {
            SendResult sendResult = producerBean.send(message);
            if (sendResult != null) {

                String message_id = sendResult.getMessageId();
                log.error("发送分消息成功 messageId", message_id);
            }
        } catch (Exception e) {
            log.error("发送消息失败", e);
        }
    }

}

